

const CHARTS = require('../../../../utils/wxcharts.js');

var apiHost = wx.getStorageSync("apiHost"); //请求地址
var Appid = wx.getStorageSync('Appid');
var Secret = wx.getStorageSync('Secret');

var AppidFlag = wx.getStorageSync('AppidFlag');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    heard : '',           //头部数据
    pretype: 'today',     //筛选数据
    categories: [],       //报表横轴数据
    dataflag:[],          //报表数据
  },

  onLoad: function (options) {
    var that = this;
    var userid = options.userid;
    var token = wx.getStorageSync('token');      //用户的token信息

    that.setData({
      token: token
    })
    // userid =  "eadd76f7be1e4d8cb335a3b123a1223d";
    that.setData({
      userid: userid,
      token: token
    })
    that.initHeard();
    that.commonRequest("/mobile/users/personal/perform/today", 'lineList');
  },

  /**
   * 初始化个人报表头部
   */
  initHeard: function (){
    var that = this;
    that.commonRequest("/mobile/users/personal/perform/head", 'heard');
  },

  /**
   * 个人报表切换筛选
   */
  preClick :function(e){
    var that = this;
    var pretype = e.currentTarget.dataset.retype
    that.setData({
      pretype: pretype
    })
    switch (pretype){
      case 'today' : 
        that.commonRequest("/mobile/users/personal/perform/today", 'lineList');
        break;
      case 'week':
        that.commonRequest("/mobile/users/personal/perform/week", 'lineList');
        break;
      case 'month':
        that.commonRequest("/mobile/users/personal/perform/month", 'lineList');
        break;
      case 'quarter':
        that.commonRequest("/mobile/users/personal/perform/quarter", 'lineList');
        break;
    }
  },

  /**
   * 通用请求
   */
  commonRequest : function(url,dataFlag){
    var that = this;
    var userid = that.data.userid
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + url, //仅为示例，并非真实的接口地址
      data: {
        userId: userid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          var data = res.data.data;
          if (dataFlag == "heard"){
            that.setData({
              heard: data
            })
            console.log(data)
          }else{
            that.analysis(data); //分析数据
          }
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 分析数据
   */
  analysis : function(data){
    var that = this;
    that.setData({
      lineList: data
    })
    var orData = data.list;
    var xData = [];
    var dataValue = [];

    for (var i = 0; orData.length > i ; i++){
      var cate = orData[i].TIME;
      var dataf = orData[i].AMOUNT;
      xData.push(cate);
      if (i == orData.length){
        dataValue.push(dataf);
      }else{
        dataValue.push(dataf);
      }
    }
    that.lineShow(xData, dataValue)
  },

  /**
   * 报表明细
   */
  lineShow: function (categories, dataflag) {
      let line = {
        animation: true, //是否有动画
        canvasId: 'lineGraph', // canvas-id
        type: 'line', // 图表类型，可选值为pie, line, column, area, ring
        categories: categories,
        series: [{ // 数据列表
          name: '趋势图',
          data: dataflag,
          color: '#F87B7A',
        }],
        yAxis: {
          min: 0, // Y轴起始值
          max :2
        },
        width: 310,
        height: 165,
        dataLabel: false, // 是否在图表中显示数据内容值
        legend: false, // 是否显示图表下方各类别的标识
        
        extra: {
          lineStyle: 'curve', // (仅对line, area图表有效) 可选值：curve曲线，straight直线 (默认)
        }
      }
    new CHARTS(line);
  },

  /**
   * 设置经理
   */
  setSales : function(){
    var that = this;
    that.commonSetRequest("mobile/users/partner/setSales");
  },

  /**
   * 设置普通用户
   */
  setSales: function () {
    var that = this;
    that.commonSetRequest("mobile/users/partner/cancelSales");
  },

  /**
   * 设置通用接口
   */
  commonSetRequest: function (url){
    var that = this;
    var token = that.data.token;
    var subYunId = that.data.userid;
    
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + url, //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag,
        subYunId: subYunId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          that.onLoad();
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 提示信息
   */
  showMsg: function (msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 2000,
      mask: true
    });
  },
})